<template>
    <el-container class="home-container">
        <!-- 头部区域 -->
        <el-header>
            <el-menu mode="horizontal">
                <div class="fl title">校园招聘系统</div>
                <div class="fr logout" style="display:flex;">
                <el-menu-item index="3">
                    <div>{{this.user.username}}</div>
                </el-menu-item>
                <el-menu-item @click="onLogout" index="2">
                    <div>退出登录</div>
                </el-menu-item>
                </div>
            </el-menu>
            
        </el-header>
        <!-- 主体区域 -->
        <el-container>
            <!-- 侧边栏 -->
            <el-aside :width="isCollapse ?'64px':'200px'">
                <div class="toggle-button" @click="toggleCollapse">|||</div>
                <!-- 侧边栏菜单区域 -->
                <el-menu
                background-color="#333744"
                text-color="#fff"
                active-text-color="#409EFF" 
                unique-opened 
                :collapse="isCollapse"
                :collapse-transition="false"
                :router='true' 
                :default-active='activePage'
                >
                
                <!-- 一级菜单 -->
                <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id" open="102">
                    <!-- 一级菜单的模板 -->
                    <template slot="title">
                        <!-- 图标 -->
                    <i :class="iconsObj[item.id]"></i>
                    <span>{{item. authName}}</span>
                    </template>
                    <!-- 二级菜单 -->
                    <el-menu-item :index="'/'+subItem.index" 
                    v-for="subItem in item.children" 
                    :key="subItem.id"
                    @click="addTab(subItem)"
                    >
                        <template slot="title">
                        <!-- 图标 -->
                        <i :class="iconsObj[subItem.id]"></i>
                        <span>{{subItem.authName}}</span>
                        </template>
                    </el-menu-item>
                </el-submenu>
                </el-menu>
          </el-aside>
          
          <!-- 右侧内容 -->
          <el-main>
            <el-tabs v-model="editableTabsValue" type="card" closable @tab-remove="removeTab">
                <el-tab-pane
                v-for="(item,name) in editableTabs"
                :key="name"
                :label="item.title"
                :name="item.name"
                :tab-click="change()">
                </el-tab-pane>
            </el-tabs>
              <!-- 路由占位符 -->
              <router-view></router-view>
          </el-main>
        </el-container>
    </el-container>
</template>
<script>
export default {
    
    data(){
        return{
            dialogVisible: false,
            ruleForm: {},
            menulist:[
                {
                    id: 101,
                    authName: "企业中心",
                    index: null,
                    children: [
                        {
                            id: 103,
                            authName: "企业信息",
                            index: "ComInformation",
                            children: []
                        },
                    ]
                },
                {
                    id: 102,
                    authName: "求职者管理",
                    index: null,
                    children: [
                        {
                            id: 105,
                            authName: "求职者列表",
                            index: 'Jobseekers',
                            children: []
                        }
                    ]
                },
                {
                    id: 103,
                    authName: "招聘管理",
                    index: null,
                    children: [
                        {
                            id: 105,
                            authName: "招聘列表",
                            index: 'ComRecruit',
                            children: []
                        }
                    ]
                },
            ],
            iconsObj:{
                '101':'iconfont icon-shouye',//首页
                '102':'iconfont icon-remen',
                '103':'iconfont icon-zuixinzixun-06',
                '104':'iconfont icon-zhiwei',
                '105':'iconfont icon-danlieliebiao',
                '106':'iconfont icon-qidongzhaopinshenqing',
                '107':'iconfont icon-zhaopinhui',
                '108':'iconfont icon-QQ',
                '109':'iconfont icon-jurassic_user',
                '110':'iconfont icon-jianli',
                '111':'iconfont icon-31guanzhu1xuanzhong',
                '112':'iconfont icon-zhaopinguanli',
                '113':'iconfont icon-baobiao',
                },
            //是否折叠
            isCollapse:false,
            //被激活的链接
            activePage:'102',

            editableTabsValue: '1',
            editableTabs: [{
                title: '首页',
                name: '1',
                content: '/ComWelcome'
            }],
            tabIndex: 1,
            user:{},
        }
    },
    created(){
        this.user = JSON.parse(window.sessionStorage.getItem('user')),
        this.activePage=window.sessionStorage.getItem('activePath')
    },
    methods:{
        onLogout() {
            window.sessionStorage.clear()
            this.$router.push('/login')
        },
        //点击按钮，切换菜单的折叠与展开
        toggleCollapse(){
            this.isCollapse=!this.isCollapse

        },
        goto (where) {
            console.log(where);
            if (where !== this.nowRoute) {
                this.$router.push(where)
            }
            this.nowRoute = where
        },
        addTab (subItem) {
            if(subItem.index==='Yonghu'){
                this.goto('Yonghu')
            }
            if (this.findeTab(subItem.index) !== false) {
                this.removeTab(this.findeTab(subItem.index))
            }
            const newTabName = ++this.tabIndex + ''
            this.editableTabs.push({
                title: subItem.authName,
                name: newTabName,
                content: subItem.index
            });
            this.editableTabsValue = newTabName
            window.sessionStorage.setItem('targetName', subItem.index)
            this.saveTab()

        },
        removeTab (targetName) {
            var tabs = this.editableTabs
            var activeName = this.editableTabsValue
            if (activeName === targetName) {
                tabs.forEach((tab, index) => {
                if (tab.name === targetName) {
                    const nextTab = tabs[index + 1] || tabs[index - 1]
                    if (nextTab) {
                    activeName = nextTab.name
                    }
                }
                })
            }
            this.editableTabsValue = activeName
            this.editableTabs = tabs.filter(tab => tab.name !== targetName)
            window.sessionStorage.setItem('targetName', targetName)
            this.saveTab()
        },
        findeTab (targetName) {
            var flag = false
            var tabs = this.editableTabs
            tabs.forEach((tab, index) => {
                if (tab.content === targetName) {
                flag = tab.name
                return flag
                }
            })
            return flag
        },
        saveTab () {
            var str = ''
            var tabs = this.editableTabs
            tabs.forEach((tab, index) => {
                str = str + tab.content + ','
            })
            window.sessionStorage.setItem('list', str)
            },
        change () {
            this.nowRoute = this.$route.path
            var tabs = this.editableTabs
            tabs.forEach((tab, index) => {
                if (tab.name === this.editableTabsValue) {
                this.goto(tab.content)
                window.sessionStorage.setItem('targetName2', tab.content)
                // console.log(window.sessionStorage.getItem('targetName'))
                }
            })
        }

    }
    
}
</script>
<style lang="less" scoped>
.el-header .fr {
  float: right;
}
.el-header .fl {
  float: left;
}
.el-header {
  width: 100%;
  color: #333;
  text-align: center;
  line-height: 60px;
  padding: 0;
  z-index: 99;
}
.logo {
  width: 60px;
  height: 60px;
  margin-left: 70px;
}
.avator {
  width: 40px;
  height: 40px;
  background: #ffffff;
  border-radius: 50%;
}
.title {
  color: #ffffff;
  font-size: 20px;
  font-weight: bold;
  margin-left: 20px;
}
.home-container{
    height: 100%;
}
.el-aside{
    background-color: #333744;
    .el-menu{
        border-right:none
    }
}
.ele-main{
    background-color: #EAEDF1;
}
.iconfont{
    margin-right: 10px;
}
.toggle-button{
    background-color: #4A5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;

}

</style>